//
//  DFOriginalView.swift
//  FlyBlog
//
//  Created by FEI on 16/4/14.
//  Copyright © 2016年 FEI. All rights reserved.
//

import UIKit

class DFOriginalView: UIView {

    var status: DFStatus? {
        didSet {
            if let profile_image_url = status?.user?.profile_image_url {
                let url = NSURL(string: profile_image_url)!
                iconView.sd_setImageWithURL(url,placeholderImage: UIImage(named: "avatar_vip"))
            }

            nameLabel.text = status?.user?.screen_name

            timeLabel.text = NSDate.date(status?.created_at ?? "刚刚").sinaDateDescription()

            sourceLabel.text = status?.source

            contentLabel.text = status?.text

            memberView.image = status?.user?.mbrankImage

            verifiedView.image = status?.user?.verifiedImage

            pictureView.pictureURLs = status?.storePictureURLs

            pictureView.snp_updateConstraints { (make) -> Void in
                let margin = status?.storePictureURLs == nil ? 0 :DFStatusCellMargin
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            }

//            if let mbrank = status?.user?.mbrank {
//                if mbrank > 0 && mbrank <= 6 {
//                    memberView.image = UIImage(named: "common_icon_membership_level\(mbrank)")
//                }else {
//                    memberView.image = nil
//                }
//            }
//
//            if let verified_type = status?.user?.verified_type {
//                switch verified_type {
//                case 0:
//                    verifiedView.image = UIImage(named: "avatar_vip")
//                case 2,3,5:
//                    verifiedView.image = UIImage(named: "avatar_enterprise_vip")
//                case 220:
//                    verifiedView.image = UIImage(named: "avatar_grassroot")
//                default:
//                    verifiedView.image = nil
//                }
//            }
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("--")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()

    }

    private func prepareUI() {
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        addSubview(pictureView)

        //添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(DFStatusCellMargin)
            make.size.equalTo(CGSize(width: DFStatusIconWH, height: DFStatusIconWH))
        }

        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(DFStatusCellMargin)
        }

        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLabel)
        }

        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(DFStatusCellMargin)
            make.centerY.equalTo(timeLabel)
        }

        memberView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(DFStatusCellMargin)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }

        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }

        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(DFStatusCellMargin)
            make.top.equalTo(iconView.snp_bottom).offset(DFStatusCellMargin)
            make.trailing.equalTo(self).offset(-DFStatusCellMargin)
        }

        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset((DFStatusCellMargin))
            make.leading.equalTo(self).offset(DFStatusCellMargin)
            make.size.equalTo(CGSizeZero)
        }

        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView).offset(DFStatusCellMargin)
        }
    }

    //MARK:懒加载
    //头像
    private lazy var iconView: UIImageView = {
      let imageView =  UIImageView(image: UIImage(named:"avatar"))
        imageView.layer.cornerRadius = 17.5
        imageView.layer.masksToBounds = true
        return imageView
    }()

    //用户名称label
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(DFStatusNameFontSize)
        label.textColor = UIColor.darkGrayColor()
        return label
    }()

    //时间label
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(DFStatusTimeFontSize)
        label.textColor = UIColor.orangeColor()
        return label
    }()

    //来源label
    private lazy var sourceLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(DFStatusTimeFontSize)
        label.textColor = UIColor.lightGrayColor()
        return label
    }()

    //会员图标
    private lazy var memberView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))

    //认真图标
    private lazy var verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))

    //原创微博
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(DFStatusContentFontSize)
        label.textColor = UIColor.darkGrayColor()
        let result = arc4random_uniform(100) % 2
        label.text = result == 0 ? "下雨了下雨了下雨了下雨了下雨了下雨了下雨了下雨了下雨了下雨了下雨了下雨了下雨了下雨了下雨了下雨了下雨了" : "天晴了天晴了天晴了天晴了天晴了天晴了天晴了天晴了天晴了天晴了天晴了天晴了天晴了天晴了天晴了天晴了天晴了天晴了天晴了天晴了天晴了"
        label.sizeToFit()
        label.numberOfLines = 0
        return label
    }()

    //原创微博配图
    private lazy var pictureView: DFStatusPictureView = DFStatusPictureView()
}
